<?php
/**
 * @author Martin Pernica
 * @copyright 2008
 */

echo '<br /><h2><a href="index.php?p=components&name=poll">'.Lang::getBackGlobal('back').'</a></h2><br />';

if (intval($_POST['answers']) || intval($_SESSION['answers'])) {
    if (!empty($_POST['question'])) {

        $question = Registry::get('db')->real_escape_string($_POST['question']);
        $answers = array();
        for ($i = 1; !empty($_POST['answer-' . $i]); $i++) {
            $answers[$i] = $_POST['answer-' . $i];
        }
        $active = intval($_POST['active']);

        $max = count($answers);
        $answers = serialize($answers);

        $vote = array();
        $vote = array_fill(1, $max, 0);
        $vote = serialize($vote);

        $ip = array(0);
        $ip = serialize($ip);

        $query = Registry::get('db')->query("INSERT INTO {prefix}poll (question, answers, vote, ip, active) VALUES('" . $question . "', '" . $answers .
            "', '" . $vote . "', '" . $ip . "', '" . $active . "')");
            $log->write(8, 'com_poll', 'NOW()', 'User created new poll', $_SESSION['id']);
			
            messageBox( Lang::getBack('com_poll', 'added_done') );
            
        echo '<meta http-equiv="refresh" content="2;url=index.php?p=components&name=poll">';
    }
    
$_SESSION['answers'] = intval($_POST['answers']);

$form = new Form;
$form->addTextbox('question', Lang::getBack('com_custom', 'question'), stripslashes($data['question']));

for ($i = 1; $i <= intval($_SESSION['answers']); $i++) {
    $form->addTextbox('answer-' . $i, Lang::getBack('com_poll', 'answer_no') . ' ' . $i);
}

$form->addCheckbox('active', Lang::getBack('com_poll', 'active'), 1);
$form->addSubmit('submit_edit', Lang::getBackGlobal('submit'));

echo $form;

} else {
    Text::h2( Lang::getBack('com_poll', 'count_small') );
    echo '<meta http-equiv="refresh" content="3;url=index.php?p=components&name=poll">';
}
?>